package com.example.monstercatch;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class CenaDeJogo extends View implements Runnable{

	private String TAG = "GameScene";
	
	private Thread mainThread;
	private int interval = 10;
	private boolean running = true;
	private GerenciadorDeJogo gerenciadorDeMonstros;
	
	public CenaDeJogo(Context context) 
	{
		super(context);	
		running = true;
		
		this.setFocusable(true);
		
		Start();	
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh)
	{
		
		MinhasCoisas.setCanvasHeight(h);
		MinhasCoisas.setCanvasWidth(w);
		gerenciadorDeMonstros.Configurar(w, h);
		
		mainThread = new Thread(this);
		mainThread.start();
		
		Log.i(TAG, "on size Changed");
	}
	
	public boolean onTouchEvent(MotionEvent event) 
	{
		switch (event.getAction()) 
		{
			case MotionEvent.ACTION_DOWN:
				gerenciadorDeMonstros.TratarEventoDeTouch();
				break;
			
		}
		return super.onTouchEvent(event);
	}
	//------------------------------------------------------------- START
	public void Start()
	{
		gerenciadorDeMonstros = new GerenciadorDeJogo();
	}
	//------------------------------------------------------------- UPDATE
	public void Update()
	{
		gerenciadorDeMonstros.Atualizar();
	}
	
	//Draw
	@Override
	public void draw(Canvas canvas) 
	{
		super.draw(canvas);
		gerenciadorDeMonstros.Desenhar(canvas);
	}
	
	public void SetAlive(boolean bool)
	{
		this.running = bool;
	}
		
	@Override
	public void run() {
		
		while(running)
		{
			try
			{
				Thread.sleep(interval);
			}
			catch(InterruptedException e)
			{
				running = false;
				Log.i(TAG, "Erro no processo de Thread", e);
			}
			
			Update();
			postInvalidate();			
		}

	}
}
